import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/Login.vue";
import Home from "../views/Home.vue";
import Welcome from "../components/Welcome.vue";
import Users from "../components/Users.vue";
import Authority from "../components/auth/Authority.vue";
import Roles from "../components/auth/Roles.vue";
import Goods from "../components/goods/Goods.vue";
import Classify from "../components/goods/Classify.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        component: Welcome,
      },
      {
        path: "/users",
        component: Users,
      },
      { path: "/authority", component: Authority },
      { path: "/roles", component: Roles },
      { path: "/goods", component: Goods },
      { path: "/classify", component: Classify },
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
// Add Access Control
router.beforeEach((to, rom, next) => {
  if (to.path === "/login") {
    return next();
  }
  const tokenStr = window.sessionStorage.getItem("token");

  return tokenStr ? next() : next("/login");
});
export default router;
